package com.cwnu.onlinemall.controller;

import com.cwnu.onlinemall.pojo.MallCategory;
import com.cwnu.onlinemall.pojo.MallSalesrecord;
import com.cwnu.onlinemall.pojo.ServerResult;
import com.cwnu.onlinemall.service.CategoryService;
import com.cwnu.onlinemall.service.CategoryServiceImpl;
import com.cwnu.onlinemall.service.SalesrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class CategoryController {
    @Value("${server.port}")
    String port;//用来保存application.yml中的server.port的值



    @Autowired
    CategoryService categoryService;

    @RequestMapping("/category/selectById")
    public ServerResult selectById(Integer id){
        MallCategory mallCategory = categoryService.selectById(id);
        return new ServerResult(0,"成功",mallCategory);


    }
    @RequestMapping("/category/insert")
    public ServerResult insert(MallCategory mallCategory){
        Integer insertRow = categoryService.insert(mallCategory);
        if (insertRow>=1){
            return new ServerResult(0,"成功",null);
        }else {
            return new ServerResult(1,"失败",null);
        }


    }
    @RequestMapping("/category/selectAll")
    public ServerResult selectAll(){
        List<MallCategory> mallCategories = categoryService.selectAll();
        return new ServerResult(0,"查询成功"+port,mallCategories);
    }

    @RequestMapping("/category/delectById")
    public ServerResult delectById(Integer id){
        Integer integer = categoryService.deleteById(id);
        if (integer>=1){
            return new ServerResult(0,"删除成功",null);

        }
        return new ServerResult(1,"删除失败",null);
    }

    @RequestMapping("/category/updateById")
    public ServerResult updateById(MallCategory mallCategory){
        Integer updateRow = categoryService.updateById(mallCategory);
        if (updateRow>=1){
            return new ServerResult(0,"修改成功",null);

        }
        return new ServerResult(1,"修改失败",null);
    }

}